<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
function cms_fieldmodel_select($modelid) {
    $db = xt_load('db','icweb');
    $models = array();
    $result = $db->query("SELECT * FROM `".xt_tname('model_field','icweb')."` WHERE `modelid`=$modelid ORDER BY `fieldid`");
    while($r = $db->fetch_array($result)) {
        $models[] = $r;
    }
    $db->free_result($result);
    return $models;
}
function cms_fieldmodel_get($fieldid) {
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname('model_field','icweb')."` WHERE `fieldid`=$fieldid");
    if(!$data) return FALSE;
    return $data;
}
function cms_fieldmodel_disable($modelid,$fieldid) {
    $db = xt_load('db','icweb');
    $r = $db->query("UPDATE `".xt_tname('model_field','icweb')."` SET `disabled` = 1-`disabled` WHERE `fieldid`=$fieldid");
    cms_fieldmodel_cache($modelid);
    return $r;
}
function cms_fieldmodel_add($modelid, $info, $setting, $tablename) {
    $db = xt_load('db','icweb');
    $tablename = xt_tname($tablename,'icweb');
    $field = addslashes($info['field']);
    if($db->result($db->query("SELECT count(*) from `".xt_tname('model_field','icweb')."` WHERE `field`='$field' AND `modelid`=$info[modelid]"))>0)
        return -3;
    $info['setting'] =  var_export(xt_stripslashes($setting), TRUE);    
    $r =  $db->insert('model_field', $info,1,'icweb');
    if( $r > 0) {
        extract($setting);
        extract($info);
        require_once MS_FIELDS_PATH.$formtype.'/field_add.inc.php';
	}
	cms_fieldmodel_cache($modelid);
    return $r;
}
function cms_fieldmodel_edit($modelid,$fieldid, $info, $setting,$formtype,$issystem,$tablename) {
    $db = xt_load('db','icweb');
    $tablename = xt_tname($tablename,'icweb');
    $field = addslashes($info['field']);
    if($db->result($db->query("SELECT count(*) from `".xt_tname('model_field','icweb')."`
        WHERE `field`='$field' AND `modelid`=$info[modelid] and fieldid<>$fieldid"))>0)
        return -3;
    $info['setting'] =  var_export(is_array($setting) ? $setting : array(), TRUE);
    $r =  $db->update('model_field', $info,"fieldid = $fieldid",'icweb');
    if( $r > 0 && intval($issystem)==0) {
        extract($setting);
        extract($info);
        require_once MS_FIELDS_PATH.$formtype.'/field_edit.inc.php';
	}
    cms_fieldmodel_cache($modelid);
    return $r;
}
function cms_fieldmodel_delete($modelid,$fieldid, $tablename) {
    $db = xt_load('db','icweb');
    $tablename = xt_tname($tablename,'icweb');
    $f = cms_fieldmodel_get($fieldid);
    $r = FALSE;
    if($f) {
        $r = $db->remove('model_field', "fieldid = $fieldid",'icweb');
        if( $r > 0 && intval($f['issystem']) == 0) {
           extract($f);
           require_once MS_FIELDS_PATH.$formtype.'/field_delete.inc.php';
        }
    }
    cms_fieldmodel_cache($modelid);
    return $r;
}

function cms_fieldmodel_cache($modelid) {
    $db = xt_load('db','icweb');
    $fields = $datasource = array();
    $result = $db->query("SELECT * FROM `".xt_tname('model_field','icweb')."` WHERE `modelid`=$modelid  AND disabled=0 ORDER BY `fieldid`");
    while($r = $db->fetch_array($result)) {
        if($r['setting']) {
            eval("\$r['setting'] = $r[setting];");
           /* $r = array_merge($r, $setting);
            unset($setting);*/
		}
        $fields[$r['field']] = $r;
        if(!$r['issystem']) $datasource[$r['field']] = $r['name'];
        
    }
    $db->free_result($result);
    xt_writecache('fields_'.$modelid.'.cache.php', $fields, MS_CACHEPATH.'/model/');
    /*if(strtolower(CHARSET) != 'utf-8') $datasource = str_charset(CHARSET, 'utf-8', $datasource);
    cache_write('phpcms_'.str_replace(DB_PRE, 'phpcms_', $this->tablename).'.php', $datasource, PHPCMS_ROOT.'data/datasource/');*/
    return true;
}
?>